const express=require('express')
const router=express.Router()
const db=require('../utrls/db')
router.post('/toData',(req,res)=>{
    const {page,count}=req.body;
    const sql=`SELECT * FROM t_notice LIMIT ${(page-1)*count},${count};`;
    const sql1=`SELECT COUNT(*) AS total FROM t_notice;`

    //执行sql 登录
    db.query(sql,(err,data)=>{
        if(err){
            res.send({error:1,mesg:err})
        }else{
            db.query(sql1,(err2,data2)=>{
                if(err2){
                    res.send({error:1,mesg:err2})
                }else{
                    res.send({err:0,data:data,total:data2[0]})
                }
            })
        }
    })
})
router.post('/getData',(req,res)=>{
    const{loginId}=req.body
    const sql=`SELECT * FROM t_student WHERE s_user=${loginId};`;
    const sql1=`SELECT * FROM t_admin WHERE a_user=${loginId};`
    
    db.query(sql,(err,data)=>{
        if(err){
            db.query(sql1,(err1,data1)=>{
                res.send({data1:data1})
            })
        }else{
            const sql2=`SELECT * FROM t_dorm WHERE d_id=${data[0].s_d_id};`
            db.query(sql2,(err2,data2)=>{
                res.send({data:data,data2:data2})
                
            })

        }
    })

})
router.post('/payment',(req,res)=>{
    const {s_id}=req.body
    const sql=`SELECT * FROM t_pay WHERE p_s_id=${s_id};`
    db.query(sql,(err,data)=>{
        res.send(data)
    })

})

//充值
router.post('/rechargeBtn',(req,res)=>{
    const {money,time,s_id,s_drom}=req.body
    const sql=`INSERT INTO t_pay VALUE(NULL,${money},'${time}',${s_id});`
    const sql1=`UPDATE t_dorm SET d_cost=d_cost+${money} WHERE d_id=${s_drom};`
    db.query(sql,(err,data)=>{
        if(!err){
            db.query(sql1,(err1,data1)=>{
                if(!err){
                    res.send(data1)
                }
            })
        }
    })
})
module.exports=router;
